﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebBrowserTest.DAL;

namespace WebBrowserTest.BL
{
   
    public class SeoBL
    {
        private SeoDAL seoDAL = new SeoDAL();
        public ChartSourceItem GetDomainPositionItems(List<MainAnalyzerResults> source)
        {
            ChartSourceItem item = new ChartSourceItem();
            
            item.XAxisValues = source.Select(o => o.CheckDate.Date).ToList();
            item.YAxisValues = source.Select(o => o.GooglePosition).ToList();
            return item;
        }

        public ChartSourceItem GetDomainSiteItems(List<MainAnalyzerResults> source)
        {
            ChartSourceItem item = new ChartSourceItem();
            item.XAxisValues = source.Select(o => o.CheckDate.Date).ToList();
            item.YAxisValues = source.Select(o => o.Site).ToList();
            return item;
        }

        public ChartSourceItem GetDomainBackLinksItems(List<MainAnalyzerResults> source)
        {
            ChartSourceItem item = new ChartSourceItem();
            item.XAxisValues = source.Select(o => o.CheckDate.Date).ToList();
            item.YAxisValues = source.Select(o => o.BackLinks).ToList();
            return item;
        }

        public string GetDomainName(List<MainAnalyzerResults> source)
        {
            return source.FirstOrDefault() == null ? string.Empty : source.FirstOrDefault().Domain;
        }

        public List<DomainHistoryListItem> GetDomainsForProject(int projectId)
        {
            List<DomainHistoryListItem> result = new List<DomainHistoryListItem>();
            var domains = seoDAL.GetDomains(projectId, true);
            foreach (var domain in domains)
            {
                result.Add(new DomainHistoryListItem{Name = domain.Domain, Id = domain.Id});
            }
            return result;
        }

        public CompareDomainsItem GetDataToCompare(int domainId, DateTime date)
        {
            var historyDate = seoDAL.GetHistoryDataToCompareForDomain(domainId, date);
            var historyDomains = seoDAL.GetHistoricalDomains(domainId).Where(o => o.CheckDate >= date).ToList();


            CompareDomainsItem compItem = new CompareDomainsItem();
            if (historyDate==null || historyDate.MainAnalyze == null || historyDate.AnalyzeDetailsTO == null) return null;
            compItem.CacheAge = historyDomains.Average(o => o.CacheAge).ToString();
            compItem.Description = historyDate.MainAnalyze.Desc;
            compItem.DMOZ = historyDate.MainAnalyze.Dmoz.ToString();
            compItem.DomainAge = historyDate.MainAnalyze.DomainAge;
            compItem.Htags = historyDate.MainAnalyze.HTags;
            compItem.PR = historyDate.MainAnalyze.PageRank.ToString();
            compItem.Title = historyDate.MainAnalyze.Title;
            compItem.Url = historyDate.MainAnalyze.Url;
            compItem.DetailsAnalyze = historyDate.AnalyzeDetailsTO.AnalyzeDetails.FirstOrDefault();
            return compItem;
        }

    }

}
